<!DOCTYPE html>
<html>
<head>
    <title>发药详情--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
    </style>
</head>
<body>

<form id="myForm" action="dispensing.html" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td>
                页号
                <input  type="text" class="abc input-default"  placeholder="" name="pageNum" value="1" /> &nbsp;
                单页条数：
                <select class="form-control" name="pageSize">
                    <option value="5" selected>5</option>
                    <option value="10">10</option>
                    <option value="15">15</option>
                </select>
                <button id="find" class="btn btn-primary" type="button">查询</button>
            </td>
        </tr>
    </table>
</form>

<table class="table table-bordered table-hover definewidth m10" >
    <thead>
    <tr>
        <th>病历号</th>
        <th>姓名</th>
        <th>药品名称</th>
        <th>未发数量</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tbody">
    <tr >
        <td style="vertical-align:middle;">1103</td>
        <td style="vertical-align:middle;">黄飞鸿</td>
        <td style="vertical-align:middle;">3402-九九感冒灵</td>
        <td style="vertical-align:middle;">3</td>
        <td style="vertical-align:middle;"><a href="#">给予全部</a>&nbsp;&nbsp;&nbsp;<a href="#">给予1</a>&nbsp;&nbsp;&nbsp;<a href="#">给予5</a>&nbsp;&nbsp;&nbsp;<a href="#">给予30</a></td>
    </tr>
    </tbody>
</table>

<table class="table table-bordered table-hover definewidth m10" >
    <tr><th colspan="5">  <div class="inline pull-right page" id="ppp">
        <span  id="total"></span> 条记录 <span id="pageNum">1</span> /<span id="pages">1</span>页
        <span name="page1" id="firstPage"><a href="javascript:void(0)">首页</a></span>
        <span name="page2" id="prePage"><a href="javascript:void(0)">上一页</a></span>
        <span name="page3" id="nextPage"><a href="javascript:void(0)">下一页</a></span>
        <span name="page4" id="lastPage"><a href="javascript:void(0)">末页</a></span>
    </div>
        <div>
            <button type="button" class="btn btn-success" onclick="black()">返回列表</button>
        </div>
    </th></tr>
</table>

<script>
    loadData();
    //填充分页信息
    function fillPageData(pageInfo) {
        //填充信息
        $("#total").html(pageInfo.total);
        $("#pages").html(pageInfo.pages);
        $("span[id='pageNum']").val(pageInfo.pageNum);
        $("select[name='pageSize']").val(pageInfo.pageSize);
        $("input[name='pageNum']").val(pageInfo.pageNum);
        //设置按钮的属性值和样式
        $("#firstPage").attr("name",1);
        $("#prePage").attr("name",pageInfo.prePage);
        $("#nextPage").attr("name",pageInfo.nextPage);
        $("#lastPage").attr("name",pageInfo.pages);
        $("#firstPage").removeClass("disabled");
        $("#prePage").removeClass("disabled");
        $("#nextPage").removeClass("disabled");
        $("#lastPage").removeClass("disabled");
        if(pageInfo.isFirstPage){//如果是第一页：上一页和首页禁用
            $("#firstPage").addClass("disabled");
            $("#prePage").addClass("disabled");
        }
        if(pageInfo.isLastPage){//如果已经是最后一页：下一页和末页禁用
            $("#nextPage").addClass("disabled");
            $("#lastPage").addClass("disabled");
        }
    }
    //加载药品数量
    function DrugNumbe(id,drugId) {
        var number;
        $.ajax({
            type: "GET",
            url: "/drugOut/getDrugNumbe",
            data: "id="+id+"&drugId="+drugId,
            async:false,
            success: function(vo){
                number=vo;
            }
        });
        return number;
    }
    //加载数据
    function loadData(){
        //获取url
        (function($){
            $.getUrlParam
                = function(name)
            {
                var reg
                    = new RegExp("(^|&)"+
                    name +"=([^&]*)(&|$)");
                var r
                    = encodeURI(window.location.search).substr(1).match(reg);
                if (r!=null) return unescape(r[2]); return null;
            }
        })(jQuery);
        var id = $.getUrlParam('id');
        var registerName = decodeURI($.getUrlParam('registerName'));
        $.ajax({
            type: "GET",
            url: "/drugOut/getRegisterDrug?id="+id,
            dataType:"json",
            success: function(vo){
                let list=vo.pageInfo.list;
                let str="";
                for(let i=0;i<list.length;i++){
                    let obj=list[i];
                    let drugId = obj.id;
                    let drugName=obj.drugName;
                    let btnStr="";
                    let number=DrugNumbe(id,drugId);
                    btnStr='<a onclick="grantDrug('+id+","+drugId+","+number+')">给予全部</a>&nbsp;&nbsp;'+
                        '<a onclick="grantDrug('+id+","+drugId+",1"+')">给予1</a>&nbsp;'+
                        '<a onclick="grantDrug('+id+","+drugId+",5"+')">给予5</a>&nbsp;' ;
                    str+='<tr >' +
                        '            <td>'+id+'</td>' +
                        '            <td>'+registerName+'</td>' +
                        '            <td>'+drugName+'</td>' +
                        '            <td>'+number+'</td>' +
                        '            <td>'+btnStr+'</td>' +
                        '</tr>';
                }
                $("#tbody").html(str);
                //填充分页数据
                fillPageData(vo.pageInfo);
            }
        });
    }

    //发放指定数量的药
    function grantDrug(id,drugId,number){
        var a = DrugNumbe(id,drugId);
        if(number>a){
            alert("给予病人的药品数大于病人需要的药品数");
            return false;
        }
        $.ajax({
            type: "POST",
            url: "/drugOut/grantDrug",
            data: {
                "id":id,
                "drugId":drugId,
                "number":number
            },
            success: function(vo){
                alert(vo.msg);
                loadData();
            }
        });
    }

    function black(){
        window.location.href="dispensing.html";
    }
</script>
</body>
</html>
